﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Group_Project_Book_Store
{
    /// <summary>
    /// Interaction logic for searchWindow.xaml
    /// </summary>
    public partial class searchWindow : Window
    {
       
        public searchWindow()
        {
            try
            {
                InitializeComponent(); 
                PopulateComboBoxes();
            }
            catch (Exception ex)
            {

                //This is the top level method so we want to handle the exception
                HandleError(MethodInfo.GetCurrentMethod().DeclaringType.Name,
                            MethodInfo.GetCurrentMethod().Name, ex.Message);
            }
            
        }
        // <summary>
        /// Method that lists the table of invoices 
        /// </summary>
       
        /// <summary>
        /// Populates the comboboxes with invoice ID, date, and total charge.
        /// </summary>
        private void PopulateComboBoxes()
        {
            try
            {
                cboInvoiceNumber.Items.Add(from invoiceNumber in DatabaseClasses.DatabaseManagment.listOfInvoices
                                           orderby invoiceNumber.invoiceID descending
                                           select invoiceNumber.invoiceID);
                cboInvoiceDate.Items.Add((from invoiceDate in DatabaseClasses.DatabaseManagment.listOfInvoices
                                         orderby invoiceDate.invoiceDate descending
                                         select invoiceDate.invoiceDate).Distinct());
                
                var listOfTotals = DatabaseClasses.DatabaseManagment.listOfInvoices.Select(i => new {
                    invoiceID = i.invoiceID,
                    total = i.listOfBooksInInvoice.Select(b => b.bookCost).Sum(),
                });

                cboInvoiceTotal.Items.Add((from invoiceTotal in listOfTotals 
                                          orderby invoiceTotal.total descending
                                          select invoiceTotal.total.ToString()).Distinct());

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Combobox for invoice date. 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboInvoiceDate_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
               // invoiceDate = (DateTime)cboInvoiceDate.SelectedItem;
               // RefreshGrid(Filter.InvoiceDate);
                
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Combobox for invoice number
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboInvoiceNumber_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
               // invoiceNumber = (int)cboInvoiceNumber.SelectedItem;
               // RefreshGrid(Filter.InvoiceNumber);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// When item is selected, enable the invoice button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                btnEditInvoice.IsEnabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Filters the data in each comboboxes for the selection made
        /// </summary>
        /// <param name="f"></param>
        //private void RefreshGrid(Filter f)
        //{
        //    try
        //    {
        //        invoiceList.Clear();
        //        IEnumerable<Invoice> filteredInvoices = new List<Invoice>();
        //        switch (f)
        //        {
        //            case Filter.InvoiceNumber:
        //                filteredInvoices = from invoice in DataRepository.GetAllInvoicesByInvoiceNum(invoiceNumber).AsEnumerable()
        //                                   select new Invoice
        //                                   {
        //                                       InvoiceId = invoice.Field<int>("InvoiceNum"),
        //                                       Date = invoice.Field<DateTime>("InvoiceDate"),
        //                                       TotalCharge = invoice.Field<decimal>("TotalCharge")
        //                                   };
        //                break;
        //            case Filter.InvoiceDate:
        //                filteredInvoices = from invoice in DataRepository.GetAllInvoicesByInvoiceDate(invoiceDate).AsEnumerable()
        //                                   select new Invoice
        //                                   {
        //                                       InvoiceId = invoice.Field<int>("InvoiceNum"),
        //                                       Date = invoice.Field<DateTime>("InvoiceDate"),
        //                                       TotalCharge = invoice.Field<decimal>("TotalCharge")
        //                                   };
        //                break;
        //            case Filter.TotalCost:
        //                filteredInvoices = from invoice in DataRepository.GetAllInvoicesByInvoiceTotal(invoiceTotal).AsEnumerable()
        //                                   select new Invoice
        //                                   {
        //                                       InvoiceId = invoice.Field<int>("InvoiceNum"),
        //                                       Date = invoice.Field<DateTime>("InvoiceDate"),
        //                                       TotalCharge = invoice.Field<decimal>("TotalCharge")
        //                                   };
        //                break;
        //            default:
        //                break;
        //        }


        //        dgvInvoiceList.ItemsSource = filteredInvoices;
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //    }
        //}
        # region Error Handling
        /// <summary>
        /// Handle the error.
        /// </summary>
        /// <param name="sClass">The class in which the error occurred in.</param>
        /// <param name="sMethod">The method in which the error occurred in.</param>
        private void HandleError(string sClass, string sMethod, string sMessage)
        {
            try
            {
                //Would write to a file or database here.
                MessageBox.Show(sClass + "." + sMethod + " -> " + sMessage);
            }
            catch (Exception ex)
            {
                System.IO.File.AppendAllText("C:\\Error.txt", Environment.NewLine +
                                             "HandleError Exception: " + ex.Message);
            }
        }
        #endregion

       

        

        
    }
}
